
local huanbi = fk.CreateSkill {
  name = "lingling__huanbi",
}

Fk:loadTranslationTable{
  ["lingling__huanbi"] = "还壁",
  [":lingling__huanbi"] = "每回合限一次，当一名角色获得另一名你与其距离为1以内的角色的牌后，你可以令获得牌的角色选择：你摸一张牌，"..
  "然后其交还获得的牌；你弃置一张牌（无牌则不弃），然后其弃置三张牌。",

  ["#lingling__huanbi-invoke"] = "还壁：是否对 %dest 发动“还壁”，令其选择一项？",
  ["lingling__huanbi1"] = "%src摸一张牌，你交还你获得的牌",
  ["lingling__huanbi2"] = "%src弃一张牌，你弃三张牌",
}

huanbi:addEffect(fk.AfterCardsMove, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(huanbi.name) and player:usedSkillTimes(huanbi.name, Player.HistoryTurn) == 0 then
      for _, move in ipairs(data) do
        if move.from and move.to and move.moveReason == fk.ReasonPrey and
          move.proposer == move.to and move.from ~= move.to and
          not move.to.dead and player:distanceTo(move.from) < 2 then
          return true
        end
      end
    end
  end,
  on_trigger = function(self, event, target, player, data)
    local room = player.room
    local targets = {}
    for _, move in ipairs(data) do
      if move.from and move.to and move.moveReason == fk.ReasonPrey and
        move.proposer == move.to and move.from ~= move.to and
        not move.to.dead and player:distanceTo(move.from) < 2 then
        table.insertIfNeed(targets, move.to)
      end
    end
    room:sortByAction(targets)
    for _, p in ipairs(targets) do
      if not player:hasSkill(huanbi.name) or player:usedSkillTimes(huanbi.name, Player.HistoryTurn) > 0 then break end
      if not p.dead then
        event:setCostData(self, {tos = {p}})
        self:doCost(event, target, player, data)
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    if room:askToSkillInvoke(player, {
      skill_name = huanbi.name,
      prompt = "#lingling__huanbi-invoke::"..to.id,
    }) then
      event:setCostData(self, {tos = {to}})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    local choice = room:askToChoice(to, {
      choices = {"lingling__huanbi1:"..player.id, "lingling__huanbi2:"..player.id},
      skill_name = huanbi.name,
    })
    if choice:startsWith("lingling__huanbi1") then
      player:drawCards(1, huanbi.name)
      if to.dead then return end
      local moves = {}
      for _, move in ipairs(data) do
        if move.from and move.to == to and move.moveReason == fk.ReasonPrey and
          move.proposer == move.to and move.from ~= move.to and not move.from.dead then
          local ids = {}
          for _, info in ipairs(move.moveInfo) do
            if table.contains(to:getCardIds("h"), info.cardId) then
              table.insertIfNeed(ids, info.cardId)
            end
          end
          if #ids > 0 then
            table.insert(moves, {
              ids = ids,
              from = move.to,
              to = move.from,
              toArea = Card.PlayerHand,
              moveReason = fk.ReasonGive,
              skillName = huanbi.name,
              proposer = move.to,
              moveVisible = false,
            })
          end
        end
      end
      if #moves > 0 then
        room:moveCards(table.unpack(moves))
      end
    else
      room:askToDiscard(player, {
        min_num = 1,
        max_num = 1,
        include_equip = true,
        skill_name = huanbi.name,
        cancelable = false,
      })
      if to.dead or to:isNude() then return end
      room:askToDiscard(to, {
        min_num = 3,
        max_num = 3,
        include_equip = true,
        skill_name = huanbi.name,
        cancelable = false,
      })
    end
  end,
})

return huanbi
